package com.gitee.fsmxm.ocon.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * @author fsmxm
 */
public class IOUtils {

    private IOUtils() {
    }

    public static String toString(InputStream inputStream, String charset) throws IOException {
        StringBuilder builder = new StringBuilder();
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, charset))) {
            String line;
            while ((line = reader.readLine()) != null) {
                builder.append(line).append('\n');
            }
            if (builder.length() > 0) {
                builder.deleteCharAt(builder.length() - 1);
            }
            return builder.toString();
        }
    }

    /**
     * 返回小写扩展名
     *
     * @param src src
     * @return 小写扩展名 or null
     */
    public static String getExt(String src) {
        if (src == null || src.trim().isEmpty()) {
            return null;
        }
        final int li = src.lastIndexOf('.');
        if (li < 0) {
            return null;
        }
        return src.substring(li + 1).toLowerCase();
    }

}
